﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QUAN_LY_NGAN_HANG_CAU_HOI_TRAC_NGHIEM
{
    public partial class FrmThemMoiGiaoVien : Form,IThemGiaoVien
    {
        ThemGiaoVienPresenter pre = new ThemGiaoVienPresenter();

        public FrmThemMoiGiaoVien()
        {
            InitializeComponent();
            pre.Init(this);
        }

        private void linkThoat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void FrmThemMoiGiaoVien_Load(object sender, EventArgs e)
        {
            cboPhai.Items.Add("Nữ");
            cboPhai.Items.Add("Nam");
            listLinhVuc.DataSource = pre.LayTatCaLinhVuc();
            listLinhVuc.ValueMember = "MaSoLinhVuc";
            listLinhVuc.DisplayMember = "ChuThich";
            listLinhVucChon.ValueMember = "MaSoLinhVuc";
            listLinhVucChon.DisplayMember = "ChuThich";
            cboQuyen.DataSource = pre.LayDanhSachQuyen();
            cboQuyen.ValueMember = "MaQuyen";
            cboQuyen.DisplayMember = "GhiChu";
            cboPhai.SelectedIndex = 0;

            this.Left = 50;
            this.Top = 0;

            this.helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
            this.helpProvider1.SetHelpKeyword(this, "ThemGiaoVien.html");
        }

        private void linkThemGiaoVien_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Guid magv = Guid.Empty;
            bool phai = true;
            Guid quyen;
            if (cboPhai.SelectedItem.ToString() == "Nữ")
                phai = false;
            try { quyen = (Guid)cboQuyen.SelectedValue; }
            catch (Exception er)
            {
                MessageBox.Show("Chua chon quyen");
                return;
            }
            magv = pre.ThemGiaoVien(txtTenGV.Text, txtHoGV.Text, txtCMND.Text, txtDiaChi.Text, txtDienThoai.Text,
                             txtNgaySinh.Text, phai, quyen, txtUser.Text, txtPass.Text.TrimEnd());

            if (magv == Guid.Empty)
            {
                MessageBox.Show("Thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                for (int i = 0; i < listLinhVucChon.Items.Count; i++)
                {
                    GiaoVienLinhVucBUS.InsertLinhVucGiaoVien(magv,((LinhVucBUS)listLinhVucChon.Items[i]).MaSoLinhVuc);
                }
                MessageBox.Show("Thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void btChon_Click(object sender, EventArgs e)
        {
            if (listLinhVucChon.Items.IndexOf(listLinhVuc.Items[listLinhVuc.SelectedIndex])==-1)
                listLinhVucChon.Items.Add(listLinhVuc.Items[listLinhVuc.SelectedIndex]);
        }

        private void btBo_Click(object sender, EventArgs e)
        {
            if (listLinhVucChon.SelectedIndex>-1)
                listLinhVucChon.Items.RemoveAt(listLinhVucChon.SelectedIndex);
        }

        private void linkXoaGiaoVien_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtTenGV.Text = "";
            txtHoGV.Text = "";
            txtPass.Text = "";
            txtCMND.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtNgaySinh.Text = "";
            txtUser.Text = "";
            listLinhVucChon.Items.Clear();
            cboQuyen.SelectedIndex = 0;
            cboPhai.SelectedIndex = 0;
        }
    }
}
